package com.jsbj.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsbj.base.WXConstant;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class WechatUtil {

    public static JSONObject getSessionKeyOrOpenId(String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<>();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
        //小程序appId
        requestUrlParam.put("appid", WXConstant.APPID);
        //小程序secret
        requestUrlParam.put("secret", WXConstant.SECRET);
        //小程序端返回的code
        requestUrlParam.put("js_code", code);
        //默认参数
        requestUrlParam.put("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam));
        return jsonObject;
    }

    public static String getPhoneNumber(String code) {
        String phone = "";
        // 获取token
        try {
            String accessToken = getToken();
            if (StringUtils.isEmpty(accessToken)) {
                log.error("获取token失败");
                return null;
            }
            log.info("token : {}", accessToken);
            //获取phone
            String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"
                    + "?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", code);
            String reqJsonStr = JsonUtil.objToString(jsonObject);
            JSONObject phoneJson = JSON.parseObject(HttpClientSslUtils.doPost(url, reqJsonStr));
            if (phoneJson.getInteger("errcode").intValue() == 0) {
                JSONObject phoneInfo = phoneJson.getJSONObject("phone_info");
                phone = phoneInfo.getString("phoneNumber");
            }

            if (phone == null) {
                log.info("获取手机号失败");
                return null;
            }
            return phone;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getToken() {
        // 获取token
        String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", WXConstant.APPID, WXConstant.SECRET);
        try {
            JSONObject token = JSON.parseObject(HttpClientUtil.doGet(token_url));
            if (token == null) {
                log.error("获取token失败");
                return null;
            }
            String accessToken = token.getString("access_token");
            if (StringUtils.isEmpty(accessToken)) {
                log.error("获取token失败");
                return null;
            }
            log.info("token : {}", accessToken);
            return accessToken;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getGZHToken() {
        String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", "wx598ebdbe2df1f5dd", "218a1d76d5f3651556a3d1ea2cb2e3a4");
        try {
            JSONObject token = JSON.parseObject(HttpClientUtil.doGet(token_url));
            if (token == null) {
                log.error("获取token失败");
                return null;
            }
            String accessToken = token.getString("access_token");
            if (StringUtils.isEmpty(accessToken)) {
                log.error("获取token失败");
                return null;
            }
            log.info("token : {}", accessToken);
            return accessToken;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
